package com.example.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.mapper.GoodsMapper;
import com.example.mapper.OrderMapper;
import com.example.mapper.ShoppingCarMapper;
import com.example.mapper.UserMapper;
import com.example.pojo.dto.ShoppingCarDto;
import com.example.pojo.entity.Order;
import com.example.pojo.entity.ShoppingCar;
import com.example.service.ShoppingCarService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Resource
    ShoppingCarMapper shoppingCarMapper;
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    OrderMapper orderMapper;

    @Override
    public void insert(ShoppingCarDto shoppingCarDto) {

        ShoppingCar shoppingCar = BeanUtil.copyProperties(shoppingCarDto, ShoppingCar.class);
        shoppingCar.setGoodsId(goodsMapper.selectByName(shoppingCarDto.getGoodsName()).getId());
        shoppingCar.setUserId(userMapper.selectByName1(shoppingCarDto.getUserName()).getId());
        shoppingCarMapper.insert(shoppingCar);

        Order order = BeanUtil.copyProperties(shoppingCarDto, Order.class);
        order.setUserId(userMapper.selectByName1(shoppingCarDto.getUserName()).getId());
        order.setOrderNum(IdUtil.getSnowflakeNextIdStr());
        order.setOrderState(false);
        order.setCreateTime(DateUtil.now());
        orderMapper.insert(order);
    }

    @Override
    public void updateShoppingCar(ShoppingCar shoppingCar) {

        shoppingCarMapper.updateShoppingCar(shoppingCar);
    }

    @Override
    public void removeById(Integer id) {

        shoppingCarMapper.removeById(id);
    }

    @Override
    public void removeBatchByIds(List<Integer> ids) {

        for (Integer id : ids){
            shoppingCarMapper.removeById(id);
        }
    }

    @Override
    public List<ShoppingCar> list() {
        List<ShoppingCar> list = shoppingCarMapper.list();
        return list;
    }

    @Override
    public ShoppingCar getById(Integer id) {
        ShoppingCar shoppingCar = shoppingCarMapper.getById(id);
        return shoppingCar;
    }

    @Override
    public PageInfo<ShoppingCar> selectByPage(Integer pageNum, Integer pageSize, String goodsName, Integer userId) {
        if (goodsName == null){
            Integer goodsId = goodsMapper.selectByName(goodsName).getId();
            PageHelper.startPage(pageNum, pageSize);
            List<ShoppingCar> list = shoppingCarMapper.selectByPage(goodsId, userId);
            return new PageInfo<>(list);
        }
        Integer goodsId = null ;
        PageHelper.startPage(pageNum, pageSize);
        List<ShoppingCar> list = shoppingCarMapper.selectByPage(goodsId, userId);
        return new PageInfo<>(list);
    }
    @Override
    public ShoppingCar getByShoppingCarNum(String shoppingCarNum) {
        ShoppingCar shoppingCar = shoppingCarMapper.getByShoppingCarNum(shoppingCarNum);
        return shoppingCar;
    }
}
